#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash lp_solve_5.5.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1
if [[ "$1" == "$2" ]]; then echo "构建路径与安装路径需为不同路径，请输入正确的路径"; exit 1; fi

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
set -e
yum install -y wget tar environment-modules


wget -t 10 -c -P $buildpath https://jaist.dl.sourceforge.net/project/lpsolve/lpsolve/5.5.2.11/lp_solve_5.5.2.11_source.tar.gz
tar xf $buildpath/lp_solve_5.5.2.11_source.tar.gz -C $buildpath && rm -rf $buildpath/lp_solve_5.5.2.11_source.tar.gz
cd $buildpath/lp_solve_5.5/lpsolve55/

sh ccc
mv $buildpath/lp_solve_5.5 $installpath/AUGUSTUS/lp_solve_5.5

cat>"$installpath/AUGUSTUS/lp_solve_5.5/lp_solve_modulefiles"<<EOF
#%Module1.0
conflict lpsolve
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set lpsolve \$pwd
setenv lpsolve \$lpsolve
prepend-path LD_LIBRARY_PATH \$lpsolve/lpsolve55/bin/ux64
EOF

